#ifndef KU6_HASH_H
#define KU6_HASH_H 1

//#include <stdint.h> 
//#include <string.h>
//#include <stdlib.h>
//#include <sys/types.h>
//#include "block_queue.h"

typedef struct hash_elt_t {
  queue_t queue;
  void *data;
  char *name; 
} hash_elt_t;

typedef struct hash_t {
   queue_t **buckets;	
   unsigned int size;
   unsigned int count;
}hash_t;

typedef struct hash_iter_t{
	hash_t       *hash;
	unsigned int cur_bucket;
	queue_t      *elt;
}hash_iter_t;

//typedef void * (hash_iter_fun_t)(hash_elt_t*);

#ifdef  __cplusplus
extern "C" {
#endif

#define hash_align(d, a)     	(((d) + (a - 1)) & ~(a - 1))

#define align_ptr(p, a)                                                         \
  (unsigned char *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1))

#define HASH_ELT_SIZE(name)         					        \
        (sizeof(void *) + hash_align((name)->key.len + 2, sizeof(void *)))

#define hash(key, c)   ((unsigned int) key * 31 + c)

void     		free_elt(void *p);
hash_t * 		hash_init(int size);
unsigned int 	hash_key(char *data, size_t len);
hash_iter_t * 	hash_iter_init(hash_t *hash,hash_iter_t *hash_iter);
hash_elt_t  *   hash_iter_next(hash_iter_t *hash_iter);
hash_elt_t  *   hash_find_elt(hash_t *hash,char *key);
void        *   hash_find(hash_t *hash,char *key);
unsigned int    hash_push(hash_t *hash,char *key,void *value);
void 	        hash_destory(hash_t *hash);

#ifdef  __cplusplus
}
#endif
#endif //KU6_HASH_H
